/*******************************************************************************
 * Copyright (c) 2009 EclipseSource and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     EclipseSource - initial API and implementation
 ******************************************************************************/
package it.rap.applications.birt.report.accounts.core;

import it.rap.applications.birt.report.accounts.internal.Activator;
import it.rap.applications.birt.report.accounts.services.BirtDownloadServiceHandler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.rap.rwt.RWT;
import org.osgi.framework.Bundle;

public class HTMLView extends BirtReportManager {

  private static final String reportDirServer = "resources/birtReportDir/";
  private static String myresourcepath = "";
  
  
 public HTMLView(String reportName){
	 super(reportName);
 }
  
  private String createHTMLReport() throws IOException {
    ByteArrayOutputStream htmlOutputStream = new ByteArrayOutputStream();
    HTMLRenderOption renderOptions = new HTMLRenderOption();
    renderOptions.setOutputFormat( HTMLRenderOption.HTML );
    renderOptions.setOutputStream( htmlOutputStream );
    
    Bundle bundle = org.eclipse.core.runtime.Platform.getBundle( Activator.BUNDLE_ID );
    URL resourceurl = FileLocator.find( bundle, new Path( "/" ), null );
    myresourcepath = FileLocator.toFileURL( resourceurl ).getPath();
    
    renderOptions.setImageDirectory( myresourcepath );
    renderOptions.setSupportedImageFormats( "PNG" );
    

    String path = myresourcepath + reportDirServer + RWT.getUISession().getId();

    // Handle the image path
    renderOptions.setImageHandler(new HTMLServerImageHandler());
    
    renderOptions.setImageDirectory(path);
    String imgUrl = new BirtDownloadServiceHandler().createDownloadUrl( RWT.getUISession().getId() );
    renderOptions.setBaseImageURL(  imgUrl  );
    
    runReport( renderOptions );
    return htmlOutputStream.toString();
  }
  @Override
  public String doReport() throws IOException {
    String content = createHTMLReport();
	return content;
  }
  
}